package com.ruoyi.web.controller.datastatistics;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datastatistics.service.IElectricityMeterHistoryDataService;
import com.ruoyi.datastatistics.service.IElectricityTerminalXbStatisticAnalysisService;
import com.ruoyi.datastatistics.service.IMeterHistoryDataService;
import com.ruoyi.datastatistics.service.ITerminalXbStatisticAnalysisService;
import com.ruoyi.datastatistics.vo.DayOrMonthDosageStatistics;
import com.ruoyi.datastatistics.vo.DosageStatistics;
import com.ruoyi.datastatistics.vo.MonthDosageGrowthRatio;
import com.ruoyi.datastatistics.vo.XbStatisticAnalysisReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/datastatistics/electricityterminalXbStatisticAnalysis")
public class ElectricityTerminalXbStatisticAnalysisController extends BaseController {

    @Autowired
    private IElectricityTerminalXbStatisticAnalysisService terminalXbStatisticAnalysisService;

    @Autowired
    private IElectricityMeterHistoryDataService meterHistoryDataService;


    //日用量和月用量统计
//    @GetMapping("/sumDayOrMonth")
//    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
//    public AjaxResult sumDayOrMonth(@RequestParam(name = "frozenType",required = false) String frozenType,
//                                    @RequestParam(name = "areaId",required = false) String areaId,
//                                    @RequestParam(name = "beginTime",required = false) String beginTime,
//                                    @RequestParam(name = "endTime",required = false) String endTime)
//    {
//        return success(terminalXbStatisticAnalysisService.sumDayOrMonth(frozenType,areaId,beginTime,endTime));
//    }

    //年用量统计
//    @GetMapping("/sumYear")
//    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
//    public AjaxResult sumYear(@RequestParam(name = "year",required = false) String year)
//    {
//        return success(terminalXbStatisticAnalysisService.sumYear(year));
//    }

    //日用量月用量统计
    @GetMapping("/DayOrMonth")
//    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
    public TableDataInfo DayOrMonth(XbStatisticAnalysisReq req) {
        startPage1();
        return getDataTable(terminalXbStatisticAnalysisService.DayOrMonth(req));
    }

    @PostMapping("/exportDayOrMonth")
    public void exportDayOrMonth(HttpServletResponse response, XbStatisticAnalysisReq req) throws ParseException {
        List<DayOrMonthDosageStatistics> list = terminalXbStatisticAnalysisService.DayOrMonth(req);
        ExcelUtil<DayOrMonthDosageStatistics> util = new ExcelUtil<DayOrMonthDosageStatistics>(DayOrMonthDosageStatistics.class);
        util.exportExcel(response, list, "用量统计");
    }

    @GetMapping("/areaDosage")
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
    public TableDataInfo areaDosage(XbStatisticAnalysisReq req) {
        startPage();
        return getDataTable(terminalXbStatisticAnalysisService.areaDosage(req));
    }

    @PostMapping("/exportAreaDosage")
    public void exportAreaDosage(HttpServletResponse response, XbStatisticAnalysisReq req) throws ParseException {
        List<DosageStatistics> list = terminalXbStatisticAnalysisService.areaDosage(req);
        ExcelUtil<DosageStatistics> util = new ExcelUtil<DosageStatistics>(DosageStatistics.class);
        util.exportExcel(response, list, "区域月用量统计");
    }

    @GetMapping("/blankDosage")
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
    public AjaxResult blankDosage(@RequestParam(value = "sheetsCode", required = false) String sheetsCode,
                                  @RequestParam(value = "beginTime", required = false) String beginTime,
                                  @RequestParam(value = "endTime", required = false) String endTime,
                                  @RequestParam(value = "orderByColumn", required = false) String orderByColumn,
                                  @RequestParam(value = "isAsc", required = false) String isAsc) {
        return success(terminalXbStatisticAnalysisService.blankDosage(sheetsCode, beginTime, endTime, orderByColumn, isAsc));
    }


    @GetMapping("/rangeDosage")
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
    public AjaxResult rangeDosage(@RequestParam(value = "sheetsCode", required = false) String sheetsCode,
                                  @RequestParam(value = "beginTime", required = false) String beginTime,
                                  @RequestParam(value = "endTime", required = false) String endTime) {
        return success(terminalXbStatisticAnalysisService.rangeDosage(sheetsCode, beginTime, endTime));
    }

    @GetMapping("/dosageUprush")
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
    public TableDataInfo dosageUprush(XbStatisticAnalysisReq req,
                                      @RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize) {
        return terminalXbStatisticAnalysisService.dosageUprush(req, pageSize, pageNum);
    }

    /**
     * 导出小表用量突增减统计结数据
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:export')")
    @Log(title = "导出小表用量突增减统计结数据", businessType = BusinessType.EXPORT)
    @PostMapping("/dosageUprushExport")
    public void export(HttpServletResponse response, XbStatisticAnalysisReq req) {
        TableDataInfo tableDataInfo = terminalXbStatisticAnalysisService.dosageUprush(req, 9999999, 1);
        ExcelUtil<MonthDosageGrowthRatio> util = new ExcelUtil<>(MonthDosageGrowthRatio.class);
        util.exportExcel(response, (List<MonthDosageGrowthRatio>) tableDataInfo.getRows(), "小表用量突增减统计结数据");
    }


//    //一个表多时段
//    @GetMapping("/oneTerminal")
//    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:list')")
//    public AjaxResult oneTerminal(@RequestParam(name = "roomId",required = false) String roomId,
//                                  @RequestParam(name = "readDates",required = false) String[] readDates)
//    {
//        return success(terminalXbStatisticAnalysisService.oneTerminal(roomId,readDates));
//    }
//
//    //多表同时段
//    @GetMapping("/multiTerminal")
//    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:list')")
//    public AjaxResult multiTerminal(@RequestParam(name = "roomId",required = false) String[] roomIds,
//                                  @RequestParam(name = "readDate",required = false) String readDate)
//    {
//        return success(terminalXbStatisticAnalysisService.multiTerminal(roomIds,readDate));
//    }
}
